/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2007>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;

namespace MyPhotoIndex.Plugins.Share.Facebook.Fb_API
{
    public partial class SelectAlbumForm : FormTemplate
    {
        private PhotoAlbums m_photoAlbums;
        private String m_defaultAlbum;
        private int m_numOfImages;
        private List<String> m_tags;
        private String m_albumId;
        private String m_newAlbumName;
        private RadioButton m_FreetextRadioButton;
        private TextBox m_freeText;

        public SelectAlbumForm(PhotoAlbums photoAlbums, String defaultAlbum, List<String> tags, int numOfImages)
        {
            InitializeComponent();
            m_photoAlbums = photoAlbums;
            m_defaultAlbum = defaultAlbum;
            m_tags = tags;
            m_numOfImages = numOfImages;

            comboBox1.SelectedValueChanged += new EventHandler(comboBox1_SelectedValueChanged);
        }

        void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.Items.Count == 0)
            {
                base.OkButtonEnabled = false;
                return;
            }
        
            photos_getAlbums_responseAlbum album = (photos_getAlbums_responseAlbum)comboBox1.SelectedItem;
            if (album != null)
            {
                if (Convert.ToInt32(album.size) + m_numOfImages > 60)
                {
                    base.OkButtonEnabled = false;
                }
                else
                {
                    base.OkButtonEnabled = true;
                }
            }
        }

        public String AlbumId
        {
            get { return m_albumId; }
        }

        public String NewAlbumName
        {
            get { return m_newAlbumName; }
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            if (SelectExistingRadioButton.Checked == true)
            {
                m_albumId = ((photos_getAlbums_responseAlbum)comboBox1.SelectedItem).aid;
            }
            else
            {
                foreach (RadioButton rb in panel1.Controls)
                {
                    if (rb.Checked == true)
                    {
                        if (rb == m_FreetextRadioButton)
                        {
                            m_newAlbumName = m_freeText.Text;
                        }
                        else
                        {
                            m_newAlbumName = rb.Text;
                        }

                        break;
                    }
                }
            }

            base.OkButton_Click(sender, e);
        }

        private void SelectAlbumForm_Load(object sender, EventArgs e)
        {
            BuildFormContents();
        }

        private void BuildFormContents()
        {
            photos_getAlbums_responseAlbum[] albums = m_photoAlbums.GetAlbums();
            if (albums != null)
            {
                comboBox1.Items.AddRange(albums);
                if (false == String.IsNullOrEmpty(m_defaultAlbum))
                {
                    foreach (photos_getAlbums_responseAlbum album in comboBox1.Items)
                    {
                        if (album.aid.CompareTo(m_defaultAlbum) == 0)
                        {
                            comboBox1.SelectedItem = album;
                            break;
                        }
                    }
                }
            }

            if (comboBox1.SelectedItem == null &&
                comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }

            BuildcreateButtons();

            if (comboBox1.SelectedItem == null)
            {
                CreateNewRadioButton.Checked = true;
            }
            else
            {
                SelectExistingRadioButton.Checked = true;
            }
        }

        private void BuildcreateButtons()
        {
            int y = -20;

            RadioButton newRadioButton = null;

            if (m_tags != null &&
               m_tags.Count > 0)
            {
                foreach (String tag in m_tags)
                {
                    if (comboBox1.FindString(tag) != -1)
                    {
                        continue;
                    }

                    newRadioButton = new RadioButton();
                    newRadioButton.Text = tag;
                    newRadioButton.Location = new Point(0, y += (newRadioButton.Height));
                    newRadioButton.Width = panel1.ClientSize.Width - 20;
                    newRadioButton.Parent = panel1;
                    newRadioButton.Visible = true;
                }
            }

            m_FreetextRadioButton = new RadioButton();
            m_FreetextRadioButton.Text = "Other:";
            m_FreetextRadioButton.Location = new Point(0, y += (m_FreetextRadioButton.Height));
            m_FreetextRadioButton.Width = 70;
            m_FreetextRadioButton.Parent = panel1;
            m_FreetextRadioButton.Visible = true;
            m_FreetextRadioButton.CheckedChanged += new EventHandler(newRadioButton_CheckedChanged);

            m_freeText = new TextBox();
            m_freeText.Location = new Point(m_FreetextRadioButton.Width + 2, y);
            m_freeText.Width = panel1.ClientSize.Width - 20 - m_FreetextRadioButton.Width;
            m_freeText.Parent = panel1;
            m_freeText.Visible = true;
            m_freeText.TextChanged += new EventHandler(newTextBox_TextChanged);

            RadioButton rb = panel1.Controls[0] as RadioButton;
            rb.Checked = true;
        }

        void newRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;

            bool hasPassedLimit = false;
            photos_getAlbums_responseAlbum album = (photos_getAlbums_responseAlbum)comboBox1.SelectedItem;
            if (album != null)
            {
                if (Convert.ToInt32(album.size) + m_numOfImages > 60)
                {
                    hasPassedLimit = true;
                }
                else
                {
                    hasPassedLimit = false;
                }
            }

            if (CreateNewRadioButton.Checked == true &&
                radioButton.Checked == true &&
                m_freeText.TextLength == 0)
            {
                base.OkButtonEnabled = false;
            }
            else
            {
                if(hasPassedLimit == false)
                {
                    base.OkButtonEnabled = true;
                }
                else
                {
                    base.OkButtonEnabled = false;
                }
            }
        }

        void newTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (m_FreetextRadioButton.Checked == false)
            {
                m_FreetextRadioButton.Checked = true;
            }

            base.OkButtonEnabled = (textBox.TextLength > 0);
        }

        private void SelectExistingRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (CreateNewRadioButton.Checked == true)
            {
                comboBox1.Enabled = false;
                panel1.Enabled = true;
            }
            else
            {
                comboBox1.Enabled = true;
                panel1.Enabled = false;
            }

            base.OkButtonEnabled = true;
            newRadioButton_CheckedChanged(m_FreetextRadioButton, new EventArgs());
        }
    }
}